package com.taiyi.renaissancemall.ui.main.goodsdetail.commentList;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;
import com.taiyi.renaissancemall.R;
import com.taiyi.renaissancemall.Utils.ToastUtils;
import com.taiyi.renaissancemall.adapter.AdapterManager;
import com.taiyi.renaissancemall.adapter.baseadapter.CommonAdapter;
import com.taiyi.renaissancemall.base.BaseActivity;
import com.taiyi.renaissancemall.bean.AllCommentListBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

//评价列表
public class CommentListActivity extends BaseActivity<CommentListView, CommentListPresent> implements CommentListView, SpringView.OnFreshListener {

    @BindView(R.id.backIv)
    ImageView backIv;
    @BindView(R.id.titleTv)
    TextView titleTv;
    @BindView(R.id.rightIv)
    ImageView rightIv;
    @BindView(R.id.rcl_ask)
    RecyclerView rclAsk;
    @BindView(R.id.springView)
    SpringView springView;
    @BindView(R.id.top)
    ConstraintLayout top;
    @BindView(R.id.noDataIv)
    ImageView noDataIv;
    private String commodityid;
    private String commodityscore;
    private int page = 1;
    private List<AllCommentListBean.CommentlistBean> data = new ArrayList<>();
    private CommonAdapter productCommentListAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_comment_list;
    }

    @Override
    public CommentListPresent initPresenter() {
        return new CommentListPresent();
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        commodityid = getIntent().getStringExtra("commodityid");
        commodityscore = getIntent().getStringExtra("commodityscore");
        titleTv.setText(getString(R.string.allComment));
        springView.setHeader(new DefaultHeader(this));
        springView.setFooter(new DefaultFooter(this));
        springView.setGive(SpringView.Give.BOTH);
        springView.setType(SpringView.Type.FOLLOW);
        springView.setListener(this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        rclAsk.setLayoutManager(linearLayoutManager);
        productCommentListAdapter = AdapterManager.getProductCommentListAdapter(this, data);
        rclAsk.setAdapter(productCommentListAdapter);
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.titleBar(top).init();
    }

    @Override
    protected void initData() {
        presenter.getCommentList(page + "", commodityid, commodityscore);
    }

    @Override
    public void initEvent() {

    }

    @Override
    public void getCommentListSuccess(AllCommentListBean allCommentListBean) {
        hideProgress();
        springView.onFinishFreshAndLoad();
        if (page == 1) {
            data.clear();
            if (allCommentListBean.commentlist == null || allCommentListBean.commentlist.size() == 0) {
                noDataIv.setVisibility(View.VISIBLE);
            } else {
                noDataIv.setVisibility(View.GONE);
            }
        }
        data.addAll(allCommentListBean.commentlist);
        productCommentListAdapter.notifyDataSetChanged();
    }

    @Override
    public void getCommentListFailure(String msg) {
        hideProgress();
        ToastUtils.showShortToast(msg);
    }

    @OnClick(R.id.backIv)
    public void onViewClicked(View view) {
        if (view.getId() == R.id.backIv) {
            finish();
        }
    }

    @Override
    public void onRefresh() {
        page = 1;
        presenter.getCommentList(page + "", commodityid, commodityscore);
    }

    @Override
    public void onLoadmore() {
        page++;
        presenter.getCommentList(page + "", commodityid, commodityscore);
    }
}
